<template>
  <el-form :model="form" ref="formRef" label-width="0">
    <el-table :data="form.tableData" border style="width: 100%">
      <el-table-column
        label="姓名"
        width="120"
        show-overflow-tooltip
        :tooltip-formatter="toolTipFun"
      >
        <template #default="{ $index }">
          <el-form-item :prop="`tableData.${$index}.name`" :rules="rules.name">
            <el-input size="small" v-model="form.tableData[$index].name" placeholder="请输入姓名" />
          </el-form-item>
        </template>
      </el-table-column>

      <el-table-column label="年龄" width="100">
        <template #default="{ $index }">
          <el-form-item :prop="`tableData.${$index}.age`" :rules="rules.age">
            <el-input-number
              size="small"
              v-model="form.tableData[$index].age"
              :min="1"
              :max="120"
              controls-position="right"
              style="width: 90px"
            />
          </el-form-item>
        </template>
      </el-table-column>

      <el-table-column label="邮箱" min-width="200">
        <template #default="{ $index }">
          <el-form-item :prop="`tableData.${$index}.email`" :rules="rules.email">
            <el-input
              size="small"
              v-model="form.tableData[$index].email"
              placeholder="请输入邮箱"
            />
          </el-form-item>
        </template>
      </el-table-column>
    </el-table>

    <el-button type="primary" @click="submit" style="margin-top: 16px"> 提交 </el-button>
    <el-button type="primary" @click="submit" style="margin-top: 16px"> 调用父组件方法 </el-button>
    <p>this指向谁</p>
  </el-form>
</template>

<script setup>
import { reactive, ref } from 'vue'

const formRef = ref()
const form = reactive({
  tableData: [
    { name: '', age: '', email: '' },
    { name: '', age: '', email: '' }
  ]
})

const rules = {
  name: [{ required: true, message: '姓名必填姓名必填姓名必填姓名必填姓名必填', trigger: 'blur' }],
  age: [
    { required: true, message: '年龄必填', trigger: 'blur' },
    { type: 'number', min: 1, max: 120, message: '1-120', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '邮箱必填', trigger: 'blur' },
    { type: 'email', message: '格式错误', trigger: 'blur' }
  ]
}

const submit = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      console.log('提交成功', form.tableData)
    } else {
      console.log('校验失败')
    }
  })
}

const toolTipFun = (data) => {
  console.log('>>>>>>>>>>>>>>>>>>>>>>>>', data)
  return '12345'
}
</script>

<style scoped lang="scss">
/* 关键：强制错误提示不换行 */
:deep(.el-form-item__error) {
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
